﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;
using System.Web;

namespace YouHP.Pages.User.Group
{
    public class InviteMember:GroupBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;
            if (IsPost)
                HandlePost();
            else HandleGet();

        }

        protected List<UserInfo> FriendList;
        protected int FriendCount;
        protected int PageIndex=1;
        protected int PageSize = 10;
        protected int PageCount = 0;
        protected string PagingString;


        protected new void HandleCommon()
        {
            this.AddLocation("圈子成员邀请", "/User/Group/GroupInvite.aspx");
            this.SetTitle("圈子成员邀请");
            this.AddScriptSrc("/support/Alpha/Js/mootools.js");

            if (GroupUserData.Role != Constants.RoleCreator && GroupUserData.Role != Constants.RoleAdmin)
            {
                this.AddErrLine("你没有邀请权限！");
                this.SetShowBackLink(true);
                return;
            }

        }
        protected new void HandleGet()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);
            FriendList = FriendBLL.Instance.GetFriendList(PageSize, PageIndex, UserData.Id, Constants.FriendPass, out FriendCount);

            PageCount = Utils.GetPageCount(FriendCount,PageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(PageIndex,PageCount,
                "/User/Group/InviteMember.aspx" + query, "page");
           
            
        }

        protected new void HandlePost()
        {

            int[] idArray = Utils.StrToIntArray(RequestTools.GetFormString("itemid"), ",");
            List<int> fids =new List<int>();
            if (idArray !=null)
            {
                foreach (int id in idArray)
                {
                    if (id <= 0) continue;
                    fids.Add(id);
                }
            }

            bool succ=true;

            foreach (int id in fids)
            {

                GroupInviteInfo info=new GroupInviteInfo();
                info.PostUserId = UserData.Id;
                info.PostUserName = UserData.UserName;
                info.ReceiveUserId = id;
                info.GroupId = GroupData.Id;
                info.PostTime = DateTime.Now;

                //bool succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id,fids.ToArray(), Constants.RoleInviting);
                succ = GroupBLL.Instance.AddGroupInvite(info);
                if (!succ) break;

            }
            if (succ)
            {
                this.AddMsgLine("邀请已经成功发出！");
                this.SetUrl("/User/Group/GroupInvite.aspx?gid="+GroupData.Id);
                this.SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("发生错误！");
                this.SetShowBackLink(true);
                return;
            }
            
            


        }


    }
}
